<html>
<head><meta charset="utf-8"><title>Please lint against `MaybeUninit::uninit().assume_init()` · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html">Please lint against `MaybeUninit::uninit().assume_init()`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="208110946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208110946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208110946">(Aug 26 2020 at 15:56)</a>:</h4>
<p>If someone uses <code>MaybeUninit::uninit().assume_init()</code> to produce any type that isn't itself MaybeUninit, it's instant UB.</p>
<p>This is detectable just based on the output type, and so the compiler should <em>really</em> not be allowing this. Even in an <code>unsafe</code> context it is never correct. This should be a warning, if not an error. It's the most common beginners mistake when using MaybeUninit.</p>



<a name="208112224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208112224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208112224">(Aug 26 2020 at 16:06)</a>:</h4>
<p>maybe open an issue so it's not lost?</p>



<a name="208113753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208113753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208113753">(Aug 26 2020 at 16:18)</a>:</h4>
<p>i can do that later today, but i usually find a zulip topic is the best way to just start a conversation</p>



<a name="208116334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208116334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208116334">(Aug 26 2020 at 16:40)</a>:</h4>
<p>In general, we should be able to do some very basic dataflow analysis to detect definitely uninitialized values.</p>



<a name="208119159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208119159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matthiaskrgr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208119159">(Aug 26 2020 at 17:03)</a>:</h4>
<p>you don't happen to describe this clippy lint <a href="https://rust-lang.github.io/rust-clippy/master/index.html#uninit_assumed_init">https://rust-lang.github.io/rust-clippy/master/index.html#uninit_assumed_init</a> , do you? :P</p>



<a name="208124615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208124615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208124615">(Aug 26 2020 at 17:49)</a>:</h4>
<p><del>Hmm, looking at that lint, the pattern where it's ok is for making arrays?  Seems like now that libs is allowing const-generics-array-methods there should just be a safe method for that.</del></p>
<p>Oh, oops, it's already there: <a href="https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#method.uninit_array">https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#method.uninit_array</a></p>



<a name="208126624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208126624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208126624">(Aug 26 2020 at 18:06)</a>:</h4>
<p>Is there any non-UB way to get an uninitialized <code>usize</code>? That is, one that is filled nondeterministically with some valid usize value rather than causing nasal demons</p>



<a name="208126846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208126846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208126846">(Aug 26 2020 at 18:08)</a>:</h4>
<p>this is the <code>freeze</code> instruction in LLVM</p>



<a name="208126866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208126866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208126866">(Aug 26 2020 at 18:08)</a>:</h4>
<p>not sure if rust exposes it, I think llvm is having trouble making it internally consistent currently</p>



<a name="208126938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208126938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208126938">(Aug 26 2020 at 18:09)</a>:</h4>
<p><a href="https://llvm.org/docs/LangRef.html#freeze-instruction">https://llvm.org/docs/LangRef.html#freeze-instruction</a></p>



<a name="208128043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208128043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208128043">(Aug 26 2020 at 18:19)</a>:</h4>
<p><span class="user-mention" data-user-id="217864">@matthiaskrgr</span> cool, let's uplift the exiting lint then.</p>



<a name="208153387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208153387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208153387">(Aug 26 2020 at 21:47)</a>:</h4>
<p>Agreed that this lint should probably be uplifted if it's almost always UB. A handful of Clippy's correctness lints should be considered for being in the compiler given that they are against UB or always-bad code.</p>



<a name="208170157"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208170157" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208170157">(Aug 27 2020 at 01:35)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/75968">https://github.com/rust-lang/rust/issues/75968</a> issue filed</p>



<a name="208448264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208448264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208448264">(Aug 29 2020 at 18:50)</a>:</h4>
<blockquote>
<p>If someone uses MaybeUninit::uninit().assume_init() to produce any type that isn't itself MaybeUninit, it's instant UB.</p>
</blockquote>
<p>FWIW, this is wrong, there are more exceptions -- see the issue.</p>



<a name="208452901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Please%20lint%20against%20%60MaybeUninit%3A%3Auninit%28%29.assume_init%28%29%60/near/208452901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Please.20lint.20against.20.60MaybeUninit.3A.3Auninit().2Eassume_init().60.html#208452901">(Aug 29 2020 at 20:37)</a>:</h4>
<p>I suppose I should have known about the unions exception, since MaybeUninit is itself just a union</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>